package cn.chencaiju.day03;

import java.util.Arrays;

import org.junit.Test;

public class ReverseArray {
	//原数组被改变
	public void reverse(int[] nums) {
		int temp=0;
		int end=nums.length-1;
		int mid=nums.length/2;
		for(int i=0;i<mid;i++) {
			temp=nums[i];
			nums[i]=nums[end];
			nums[end--]=temp;
		}
		System.out.println("反转效果："+Arrays.toString(nums));		
	}
	//原数组不变
	public int[] reverseNewArray(int[] nums) {
		int end=nums.length;
		int mid=end/2;
		int[] newArray=new int[end];
		int temp=0;
		for(int i=0;i<end;i++) {
			newArray[i]=nums[i];
		}
		for(int i=0;i<mid;i++) {
			temp=newArray[i];
			newArray[i]=newArray[end-1];
			newArray[-1+end--]=temp;
		}
		System.out.println("反转效果："+Arrays.toString(newArray));	
		return newArray;
	}
	@Test public void testReverse() {
		int[] nums=new int[10];
		for(int i=0;i<nums.length;i++) {
			nums[i]=(int) (Math.random()*100);
		}
		System.out.println("原数组："+Arrays.toString(nums));
		reverse(nums);
	}
	@Test public void testReverseNewArray() {
		int[] nums=new int[10];
		for(int i=0;i<nums.length;i++) {
			nums[i]=(int) (Math.random()*100);
		}
		System.out.println("原数组："+Arrays.toString(nums));
		reverseNewArray(nums);
		System.out.println("反转之后，原数组："+Arrays.toString(nums));
	}
}
